package com.example.everying.service.content.impl;/*
 * 项目名称: everything
 * 文件名: PublishTemplateServiceImpl
 * 作者: 张泽阳
 * 日期: 2025/8/8
 * 描述:
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.everying.context.UserContext;
import com.example.everying.mapper.content.PublishTemplateMapper;
import com.example.everying.model.dto.content.PublishTemplatePageDto;
import com.example.everying.model.entity.content.PublishTemplate;
import com.example.everying.model.vo.user.UserInfoVo;
import com.example.everying.service.content.PublishTemplateService;
import com.example.everying.utils.Result;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class PublishTemplateServiceImpl extends ServiceImpl<PublishTemplateMapper, PublishTemplate> implements PublishTemplateService {
    @Override
    public Result<?> addTemplate(PublishTemplate publishTemplate) {
        if (publishTemplate.getTemplateName() == null){
            return Result.fail("模版名称不能为空");
        }
        if (publishTemplate.getTitle() == null){
            return Result.fail("标题不能为空");
        }
        if (publishTemplate.getContent() == null){
            return Result.fail("内容不能为空");
        }
        if (publishTemplate.getContents() == null){
            return Result.fail("富文本内容不能为空");
        }
        if (publishTemplate.getPublishType() == null){
            return Result.fail("发布类型不能为空");
        }


        UserInfoVo user = UserContext.getUser();
        int count = baseMapper.getCount(user.getId());
        if (count >= 6){
            return Result.fail("超过模版数量限制");
        }
//        publishTemplate.setUserId(user.getId());
//        if (user.getIsAdministrator() == 1){
//            publishTemplate.setType(0);
//        } else {
//            publishTemplate.setType(1);
//        }
        publishTemplate.setUserId(UserContext.getUser().getId());
        publishTemplate.setType(1);
        publishTemplate.setUseTime(LocalDateTime.now());
        publishTemplate.setCreateTime(LocalDateTime.now());
        publishTemplate.setUpdateTime(LocalDateTime.now());
        int insert = baseMapper.insert(publishTemplate);
        if (insert > 0){
            return Result.ok(publishTemplate);
        }

        return Result.fail("添加模板失败");
    }

    @Override
    public Result<?> updateTemplate(PublishTemplate publishTemplate) {

        if (publishTemplate.getType() == 0){
            PublishTemplate newTemplate = new PublishTemplate();
            newTemplate.setUserId(UserContext.getUser().getId());
            newTemplate.setTemplateId(publishTemplate.getId());
            newTemplate.setTemplateName(publishTemplate.getTemplateName());
            newTemplate.setTitle(publishTemplate.getTitle());
            newTemplate.setContent(publishTemplate.getContent());
            newTemplate.setContents(publishTemplate.getContents());
            newTemplate.setOrdersType(publishTemplate.getOrdersType());
            newTemplate.setInformation(publishTemplate.getInformation());
            newTemplate.setPublishType(publishTemplate.getPublishType());
            newTemplate.setType(1);
            newTemplate.setUseTime(LocalDateTime.now());
            newTemplate.setUpdateTime(LocalDateTime.now());
            newTemplate.setCreateTime(LocalDateTime.now());
            int insert = baseMapper.insert(newTemplate);
            if (insert > 0){
                return Result.ok(newTemplate);
            }
        }
        publishTemplate.setUseTime(LocalDateTime.now());
        publishTemplate.setUpdateTime(LocalDateTime.now());
        int update = baseMapper.updateById(publishTemplate);
        if (update > 0){
            return Result.ok(publishTemplate);
        }
        return Result.fail("修改模板失败");
    }

    @Override
    public Result<?> deleteTemplateById(String id) {
        PublishTemplate publishTemplate = baseMapper.selectById(id);
        if (publishTemplate.getType() == 0){
            PublishTemplate newTemplate = new PublishTemplate();
            newTemplate.setUserId(UserContext.getUser().getId());
            newTemplate.setTemplateName(publishTemplate.getTemplateName());
            newTemplate.setTitle(publishTemplate.getTitle());
            newTemplate.setContent(publishTemplate.getContent());
            newTemplate.setContents(publishTemplate.getContents());
            newTemplate.setOrdersType(publishTemplate.getOrdersType());
            newTemplate.setInformation(publishTemplate.getInformation());
            newTemplate.setPublishType(publishTemplate.getPublishType());
            newTemplate.setType(1);
            newTemplate.setUpdateTime(LocalDateTime.now());
            newTemplate.setCreateTime(LocalDateTime.now());
            newTemplate.setTemplateId(publishTemplate.getId());
            newTemplate.setUseTime(LocalDateTime.now());
            newTemplate.setIsDelete(1);
            int insert = baseMapper.insert(newTemplate);
            if (insert > 0){
                return Result.ok("删除模版成功");
            }
        }
        if (!publishTemplate.getUserId().equals(UserContext.getUser().getId())){
            return Result.fail("不能删除别人的模版");
        }
        publishTemplate.setIsDelete(1);
        int i = baseMapper.updateById(publishTemplate);
        if (i > 0){
            return Result.ok("删除模版成功");
        }
        return Result.fail("删除模版失败");
    }

    @Override
    public Result<?> templateList() {
        UserInfoVo user = UserContext.getUser();
        List<PublishTemplate> publishTemplateList = baseMapper.getPublishTemplateList(user.getId());
        if (publishTemplateList != null){
            return Result.ok(publishTemplateList);
        }
        return Result.fail("暂无模版数据");
    }

    // Result<?> templateList(PublishTemplate templateListByPublishType);

    @Override
    public Result<?> templateListByPublishType(PublishTemplate templateListByPublishType) {
        // System.out.println("templateListByPublishType.getPublishType()"+templateListByPublishType.getPublishType());
        UserInfoVo user = UserContext.getUser();
         List<PublishTemplate> publishTemplateList = new ArrayList<>();
        if(templateListByPublishType.getPublishType() == null){
            publishTemplateList = baseMapper.getPublishTemplateList(user.getId());
        }
        else if(templateListByPublishType.getPublishType() == 0){//单独判断是否是0，发日常帖子的模板
            publishTemplateList = baseMapper.getPublishTemplateList1(user.getId());
        }else{
            publishTemplateList = baseMapper.getPublishTemplateListByPublishType(user.getId(),templateListByPublishType.getPublishType());
        }
        
        if (publishTemplateList != null){
            return Result.ok(publishTemplateList);
        }
        return Result.fail("暂无模版数据");
    }

    @Override
    public Result<?> selectPage(PublishTemplatePageDto publishTemplatePageDto) {
        LambdaQueryWrapper<PublishTemplate> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(PublishTemplate:: getType,0);
        Page<PublishTemplate> page = new Page<>(publishTemplatePageDto.getPage(),publishTemplatePageDto.getLimit());
        Page<PublishTemplate> pageResult = baseMapper.selectPage(page, queryWrapper);
        if (pageResult != null){
            return Result.ok(pageResult);
        }
        return Result.fail("暂无数据");
    }

    @Override
    public Result<?> useTemplate(PublishTemplate publishTemplate) {
        PublishTemplate template = baseMapper.selectById(publishTemplate.getId());
        // 如果使用的是官方模版 给用户新增一条
        if (template.getType() == 0){
            PublishTemplate newTemplate = new PublishTemplate();
            newTemplate.setUserId(UserContext.getUser().getId());
            newTemplate.setTemplateName(template.getTemplateName());
            newTemplate.setTitle(template.getTitle());
            newTemplate.setContent(template.getContent());
            newTemplate.setContents(template.getContents());
            newTemplate.setOrdersType(template.getOrdersType());
            newTemplate.setInformation(template.getInformation());
            newTemplate.setPublishType(template.getPublishType());
            newTemplate.setType(1);
            newTemplate.setUpdateTime(LocalDateTime.now());
            newTemplate.setCreateTime(LocalDateTime.now());
            newTemplate.setTemplateId(template.getId());
            newTemplate.setUseTime(LocalDateTime.now());
            int insert = baseMapper.insert(newTemplate);
            if (insert > 0){
                return Result.ok("修改模版使用时间成功");
            }
        }
        template.setUseTime(LocalDateTime.now());
        int update = baseMapper.updateById(template);
        if (update > 0){
            return Result.ok("修改模版使用时间成功");
        }
        return Result.fail("修改模版使用时间失败");
    }
}
